package com.warmheart.base.filter;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import com.warmheart.core.constant.Globals;
import com.warmheart.core.util.JsonUtil;

public class ParamLogRequestWrapper extends HttpServletRequestWrapper {

    private String requestBody = null;

    private Map<String, String[]> parameterMap;

    public ParamLogRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        parameterMap = request.getParameterMap();
        requestBody = readBody(request);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return parameterMap;
    }

    @Override
    public String[] getParameterValues(String name) {
        if (MapUtils.isNotEmpty(parameterMap)) {
            return parameterMap.get(name);
        }
        return null;
    }

    @Override
    public BufferedReader getReader() {

        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() {

        final ByteArrayInputStream bais = new ByteArrayInputStream(requestBody.getBytes(StandardCharsets.UTF_8));
        return new ServletInputStream() {

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }

            @Override
            public int read() throws IOException {
                return bais.read();
            }
        };
    }

    private String readBody(ServletRequest request) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line);
        }
        return sb.toString();
    }

    public String getRequestBody() {
        return requestBody;
    }

    public String getParamInfo() throws Exception {
        Map<String, String> paramInfo = new HashMap<String, String>();
        if (parameterMap != null && !parameterMap.isEmpty()) {
            for (String paramKey : parameterMap.keySet()) {
                String[] values = parameterMap.get(paramKey);
                if (values != null && values.length > 0) {
                    String valueStr = Arrays.stream(values).filter(v -> StringUtils.isNotBlank(v))
                            .collect(Collectors.joining(Globals.COMMA));
                    if (StringUtils.isNotBlank(valueStr)) {
                        paramInfo.put(paramKey, valueStr);
                    }
                }
            }
        }
        return JsonUtil.getJsonStr(paramInfo);
    }
}
